package com.meixiang.dialog;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.meixiang.R;
import com.meixiang.adapter.shopping.DeliveryMethodAdapter;
import com.meixiang.entity.shopping.TransportEntity;
import com.meixiang.recyclerview.OnItemClick;

import java.util.ArrayList;

/**
 * 选择配送方式Dialog
 *
 * @author LZRUI
 */
public class DeliveryMethodDialogFragment extends BaseDialogFragment implements OnItemClick {

    public static String TAG = "DeliveryMethodDialogFragment";
    private static String LIST = "list";

    private RecyclerView ryMethod;
    private ArrayList<TransportEntity> dataList;
    private TransportChangeListener listener;

    public DeliveryMethodDialogFragment setTransportChangeListener(TransportChangeListener listener) {
        this.listener = listener;
        return this;
    }

    public static DeliveryMethodDialogFragment newInstance(ArrayList<TransportEntity> list) {
        DeliveryMethodDialogFragment fragment = new DeliveryMethodDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(LIST, list);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (Activity)context;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            dataList = getArguments().getParcelableArrayList(LIST);
        }
    }

    @Override
    protected int setFragmentViewId() {
        return R.layout.dialog_select_delivery_method;
    }

    @Override
    protected void initView(View rootView) {
        ryMethod = (RecyclerView) rootView.findViewById(R.id.ry_method);
        rootView.findViewById(R.id.tv_cancel).setOnClickListener(this);
    }

    @Override
    protected void initData() {
        ryMethod.setLayoutManager(new LinearLayoutManager(mActivity));
        ryMethod.setAdapter(new DeliveryMethodAdapter(mActivity, dataList, this));
    }

    @Override
    public void onClick(View v) {
        dismiss();
    }

    @Override
    public void onItemClick(View view, int position) {
        if (listener != null) {
            dismiss();
            listener.onTransportChange(position);
        }
    }

    /**
     * 快递方式改变监听器
     */
    public interface TransportChangeListener {
        void onTransportChange(int position);
    }
}
